package com.mingweicai.intergration.thread.pc1;

/**
 * 生产者
 */
public class Chef implements Runnable{
  private Restaurant restaurant;
  private int count = 0;

  public Chef(Restaurant restaurant) {
    this.restaurant = restaurant;
  }

  @Override
  public void run() {

    while (!Thread.interrupted()){
      try {
        // 等待服务员上完菜
        synchronized (this){
          while(restaurant.meal != null){
            wait();
          }
        }

        System.out.println("Chef produce the meal ");
        if(++count == 10){
          System.out.println("Close the restaurant");
          restaurant.executor.shutdownNow();
        }
        // 厨师准备好伙食，通知服务员
        synchronized (restaurant.waitPerson){
          restaurant.meal = new Meal(10);
          restaurant.waitPerson.notifyAll();
        }
      }catch (InterruptedException e){
        e.printStackTrace();
      }
    }
  }
}
